package messagecontroller;
 
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.TypeUtils;
import com.mysql.jdbc.PreparedStatement;

import util.DBConnection;

@Controller
public class Edit_UserController {
	@RequestMapping(value = "/modify_user.action", method={RequestMethod.GET}, produces = "text/html; charset=UTF-8") 
    @ResponseBody
    public String modify_user(@RequestParam Map<String, Object> input) {
	JSONObject json = new JSONObject();
	String _id = (String) input.get("id");
	String _state = (String) input.get("state");
    String st = "冻结";

	DBConnection mysql = new DBConnection();
	Connection connection = mysql.getConnection();
	if (connection == null) {
	        json.put("code", 1);
	        json.put("msg", "获取数据库Connection失败");
 	       return JSON.toJSONString(json);
     	}

    if(_state.equals(st)){
	    try {
	    	String sql1 = "update  message set state = '正常' where id = "+_id+" ;";
	    	PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement(sql1);
		    preparedStatement.executeUpdate();
		 
	    } catch (Exception e) {
		   e.printStackTrace();
		   json.put("code", 1);
		   json.put("msg", "更新用户信息失败！");
		   return JSON.toJSONString(json);
	    }
	    json.put("code", 0);
        json.put("state", "正常");
	    return JSON.toJSONString(json); 
    }else {
		 try {
		    	String sql2 = "update  message set state = '冻结' where id = "+_id+" ;";
		    	PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement(sql2);
			    preparedStatement.executeUpdate();
			    
		    } catch (Exception e) {
			   e.printStackTrace();
			   json.put("code", 1);
			   json.put("msg", "更新用户信息失败！");
			   return JSON.toJSONString(json);
		    }
		 	json.put("code", 0);
		 	json.put("state", "冻结");
		    return JSON.toJSONString(json);
		    
	}
    }
}



